<?php

/**
 * Classe help stores data temporarily, it's include sessions and cookies
 * @author Edio Hanoway Kepke
 * @version 1.0
 * @access public 
 */

class helpSDT 
{
        
    /**
    * Method that creates one or various cookies, depending of lenght string and delimiter
    * @access public
    * @param $name String Cookie name; 
    * @param $value String Data of the cookie;
    * @param $delimiter int Length maximus of the cookie;                        
    * @return void
    */    
    public static  function setCookie($name,$value, $delimiter = 2500)
    {                                        
        //split the string
        $arraytest = str_split($value, $delimiter);

        
        $array = array(
                            'lenght' => strlen($value),
                            'size' => count($arraytest)
                      );
        
        $properties = json_encode($array);
        
        
        $cookie = array(
                                'name'   => $name.'_size',
                                'value'  => $properties,
                                'expire' => '0',                                                                                                                           
                        );
                        
        $CI =& get_instance();
        $CI->load->helper('cookie');

        //create help cookie with informations of the $value
        set_cookie($cookie);                                                    
        
        $c = 0;
        
        //create others cookies  containing the values ​​splitted of $value 
        foreach ($arraytest as $value) 
        {
            $cookie = array(
                                'name'   => $name.$c,
                                'value'  => $value,
                                'expire' => '0',                               
                            );

            set_cookie($cookie);                                   
        }                    
    }
    
    
    /**
    * Method that recovers cookies saved
    * @access public
    * @param $name String Cookie Name;
    * @return void
    */ 
    public static function getCookie($name)
    {                
        $CI =& get_instance();
        
        $CI->load->helper('cookie');        
        
        $size_cookie = get_cookie($name.'_size');
                        
        $size_cookie = json_decode($size_cookie);
                                
        $count = 0;
        
        $text = false;                        
                                      
        if(is_object($size_cookie))
        {                               
            $text;
            
            $tamanho =  (int)$size_cookie->size;
            
            while($count < $tamanho)
            {             
                
                $namecookie = $name.$count;
                
                $auxtexto = get_cookie($namecookie);
                $text =  $text.$auxtexto;            
                $count++;
            }
        }    
        
        return $text;                
    }
    
    
    /**
    * Method that deleted cookies saved
    * @access public
    * @param $name String Cookie name;
    * @return void
    */ 
    public static function delCookie($name)
    {
        $CI =& get_instance();
        
        $CI->load->helper('cookie');        
                
        
        $size_cookie = get_cookie($name.'_size');
        
        delete_cookie($name.'_size');
                        
        $size_cookie = json_decode($size_cookie);
                                
        $count = 0;
        
        $texto = false;                        
                                      
        if(is_object($size_cookie))
        {                               
            $texto;
            
            $tamanho =  (int)$size_cookie->size;
            
            while($count < $tamanho)
            {             
                
                $namecookie = $name.$count;                
                delete_cookie($namecookie);
            }
        }                             
    }
    
            
    /**
    * Method that simulates Properties, for set or get sessions
    * @access public
    * @param $name String Session name ;
    * @param $param Array optional;     
    * @return boolean or Session Object
    */
    public static  function sessions($name, $param = false)
    {
        $CI = &get_instance(); 
                        
        if(!$param)
        {            
            return $CI->session->userdata($name);
        }
        else
        {
            if(is_array($param))
            {
                return $CI->session->set_userdata($param);                                            
            }
            return false;
        }                                                         
    }                        
}

?>
